package ImplementInterface;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import DatabaseInterface.WebInterface;
import HibernateFacet.*;




public class ImplementWebInterface implements WebInterface{
	public SessionFactory sessionFactory;
	public Session session;
	public  List list =new ArrayList<Object>();
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public int LoginVerified(String name, String password) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Map getUserMessage(String userName, String password) {
		Map map=new HashMap<String, Object>();
		Session session = sessionFactory.openSession();

		List list = session.createSQLQuery("select *from UserInformation where name=? and password=?")
				.addEntity(UserInformation.class)
				.setParameter(0, userName)
				.setParameter(1, password)
				.list(); 
		if(list.size()>0){
			map.put("flag", ((UserInformation)list.get(0)).getId());
			map.put("privilege", ((UserInformation)list.get(0)).getPrivilege());
		}else{
			map.put("flag", "fail");
		}
		session.close();
//		System.out.println(list);
		return map;		
	}
	
	public List getWarningNumber(int Privilege) {
		List jsonList = new ArrayList<Map>();
		
		if(Privilege==1) {
			jsonList=getAreaWarningNumber(new String[] {"佛山市禅城区","佛山市南海区"});
		}//if end
		
		return jsonList;
	}
	
	public List getAreaWarningNumber(String[] areas) {
		int warning1 = 0;
		int warning2 = 0;
		List jsonList = new ArrayList<List>();
		List areaList = new ArrayList<Map>();
		List typeList = new ArrayList<Map>();
		List deviceList = new ArrayList<Map>();
		Map map = new HashMap<String, Object>();
		Session session = sessionFactory.openSession();
		LocalDate now = LocalDate.now();
		if(areas.length>0) {
			for (String area : areas) {
				try {
					List list = session.createSQLQuery("select DeviceInformation_id,any_value(id) as id,Type,any_value(DateTime) as DateTime "
							+ "from (select * from Warning order by DateTime desc limit 1000000000) w where w.DeviceInformation_id in (select any_value(d.id) "
							+ "from InstallationManagement i left join DeviceInformation d on d.InstallationManagement_id = i.id where i.Area like '%" + area + "' "
							+ "group by d.SeriesNumber) and w.DateTime between  '" + now.plusDays(-29).toString() + "' and  '" + now.plusDays(1).toString()+"' group by w.DeviceInformation_id,w.Type")
						    .addEntity(Warning.class).list();
					if (list.size() > 0) {
						map = new HashMap<String, Object>();
						map.put("name", area);
						map.put("num", list.size());
						areaList.add(map); 
						for (int i = 0; i < list.size(); i++) {
							Warning jd = (Warning) list.get(i);
//							DeviceInformation aa = (DeviceInformation) jd.getDeviceInformation();
							List list_device=session.createSQLQuery("select *from DeviceInformation where id='" + jd.getDeviceInformation().getId() + "'").addEntity(DeviceInformation.class).list();
							DeviceInformation aa = (DeviceInformation) list_device.get(0);

							map = new HashMap<String, Object>();
							map.put("id", jd.getId());
							map.put("type", jd.getType());
							map.put("area", area);
							map.put("address", aa.getAddress());
							map.put("PositionX",aa.getPositionX());
							map.put("PositionY",aa.getPositionY());
							deviceList.add(map);
							
							if (jd.getType() == 1) warning1++;
							else warning2++;
						}
					}
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			map = new HashMap<String, Object>();
			map.put("warning1", warning1);
			map.put("warning2", warning2);
			typeList.add(map);
		}//if privilege1 end
		jsonList.add(areaList);
		jsonList.add(typeList);
		jsonList.add(deviceList);
		session.close();
		return jsonList;
	}

	
	
	public List getAreaWarningData(String startdate, String stopdate, String area, int pageCount) {
		List jsonList = new ArrayList<Map>();
		Session session = sessionFactory.openSession();
//		stopdate = stopdate + " " + "23:59:59";
//		System.out.println(area);
		try {	
			List list = session
					.createSQLQuery("select * from Warning where DeviceInformation_id in (select any_value(d.id) from "
							+ "InstallationManagement i left join DeviceInformation d on d.InstallationManagement_id = i.id "
							+ "where " + area + " group by d.SeriesNumber) and DateTime between  '" + startdate + "' and  '" + stopdate+"' ")
					.addEntity(Warning.class).list();
			Map map = new HashMap<String, Object>();
			if (list.size() > 0) {
				float f = (float) list.size() / 7;
				int count = (int) f;
				String s = f + "";
				int position = Integer.valueOf(s.substring(s.indexOf(".") + 1));
				if (position > 0)
					count += 1;
				map.put("subpage", pageCount + 1);
				map.put("page", count);
				map.put("total", list.size());
				jsonList.add(map);
				pageCount = (pageCount - 1) * 7;
				list = session.createSQLQuery("select * from Warning where DeviceInformation_id in (select any_value(d.id) from "
						+ "InstallationManagement i left join DeviceInformation d on d.InstallationManagement_id = i.id "
						+ "where " + area + " group by d.SeriesNumber) and DateTime between  '" + startdate + "' and  '" + stopdate
						+"' order by id asc limit " + pageCount + ",7 ").addEntity(Warning.class).list();
			} else {
				map.put("subpage", 0);
				map.put("page", 0);
				map.put("total", 0);
				jsonList.add(map);
			}
	
			for (int i = 0; i < list.size(); i++) {
				Warning jd = (Warning) list.get(i);
				map = new HashMap<String, Object>();
				int device_id=jd.getDeviceInformation().getId();
				List list_device=session.createSQLQuery("select *from DeviceInformation where id='" + device_id + "'").addEntity(DeviceInformation.class).list();
				DeviceInformation aa = (DeviceInformation) list_device.get(0);
				int install_id=aa.getInstallationManagement().getId();			
				List list_install=session.createSQLQuery("select *from InstallationManagement where id='" + install_id + "'").addEntity(InstallationManagement.class).list();
				InstallationManagement bb=(InstallationManagement) list_install.get(0);
				
				map.put("id", jd.getId());
				map.put("type", jd.getType());
				map.put("SeriesNumber", aa.getSeriesNumber());
				map.put("area", bb.getArea());
				map.put("address", aa.getAddress());
				map.put("Datetime",
						new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(jd.getDateTime()));
				jsonList.add(map);
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}		
	
		session.close();
		return jsonList;
	}
	public List getWarningData(String startdate, String stopdate, int Privilege, int pageCount) {
		List jsonList = new ArrayList<Map>();
	
		stopdate = stopdate + " " + "23:59:59";
		
		if(Privilege==1) {
			jsonList=getAreaWarningData( startdate,  stopdate,"i.Area like '%佛山市南海区' or i.Area like '%佛山市禅城区'",  pageCount);
		}//if end
		
		return jsonList;
	}
}
